package com.example.demo.web;

import java.util.List;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.example.demo.dao.UserDao;
import com.example.demo.entity.SysUser;

@RestController
@RequestMapping("/oneActiviController")
public class OneActiviController {

	@Autowired
	private UserDao userDao;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private TaskService taskService;
	
	private SysUser getUser(){
		SysUser user = userDao.findById("3490e627f4054754a1dda7d798234d58").get();
		return user;
	}
	
	@RequestMapping(value = "list")
	public ModelAndView list(){
		SysUser user = getUser();//假设登录人
		List<Task> list = taskService.createTaskQuery().taskAssignee(user.getName()).list();
		String html = "";
		for (Task task : list) {
			html+=task.getAssignee()+":</br>";
			html+=task.getId()+"</br>";  
            html+=task.getName()+"</br>";  
            html+=task.getCreateTime()+"</br>";  
            html+=task.getProcessInstanceId()+"</br>";  
            html+=task.getExecutionId()+"</br>";  
            html+=task.getProcessDefinitionId()+"</br>";  
		}
		return new ModelAndView("doMyActiviti.html");
	}
	
	@RequestMapping(value = "doActiviti")
	public String doActiviti(){
		String id = taskService.createTaskQuery().taskAssignee(getUser().getName()).list().get(0).getId();
		taskService.complete(id);
		return "success";
	}
}
